package com.mk.task;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mk.entity.Insurance;
import com.mk.entity.Pet;
import com.mk.entity.Vaccine;
import com.mk.mapper.InsuranceMapper;
import com.mk.mapper.PetMapper;
import com.mk.mapper.VaccineMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 1年查证宠物证件是否正常
 */
@Component
@RequiredArgsConstructor
public class CertificateTask {

    private final PetMapper petMapper;
    private final VaccineMapper vaccineMapper;
    private final InsuranceMapper insuranceMapper;

    @Scheduled(cron = "${task.checkCertificateScheduled.cron}")
    @Transactional
    public void checkCertificate() {
        LocalDateTime now = LocalDateTime.now();

        petMapper.update(new LambdaUpdateWrapper<Pet>()
                .set(Pet::getFeedStatus, Pet.ABNORMAL)
                .le(Pet::getFeedTime, now.minusYears(1)));

        petMapper.update(new LambdaUpdateWrapper<Pet>()
                .set(Pet::getAnimalStatus, Pet.ABNORMAL)
                .le(Pet::getAnimalTime, now.minusYears(1)));

        vaccineMapper.update(new LambdaUpdateWrapper<Vaccine>()
                .set(Vaccine::getStatus, Vaccine.ABNORMAL)
                .le(Vaccine::getUpdateTime, now.minusYears(1)));

        insuranceMapper.update(new LambdaUpdateWrapper<Insurance>()
                .set(Insurance::getStatus, Insurance.ABNORMAL)
                .le(Insurance::getUpdateTime, now.minusYears(1)));
    }

}
